********************************************************************************
** 	TITLE:   f2_community_survey_balance.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Table IV: IGNITE Exposure Balance Test: Community Survey

********************************************************************************
********************************************************************************
		set sortseed 13

		//ssc install schemepack, replace
			set scheme white_viridis
		//ssc install scheme_tufte, replace
			set scheme tufte
		ssc install ietoolkit, replace //Package needed for command iebaltab
		//ssc install estout, replace //Package needed for command eststo
		graph set window fontface "Times New Roman"
		//ssc install scheme-burd, replace
		//https://github.com/briatte/burd
		set scheme burd4
		//ssc install reghdfe, replace
		//ssc install ftools, replace
	
		//Running ado file for version 6.4 of ietoolkit from local dropbox folder for iebaltab
		local currentuser = c(username)
		capture quietly do "/Users/`currentuser'/Dropbox/ietoolkit-6.4/src/ado_files/iebaltab.ado"


********************************************************************************
* Import Data
********************************************************************************
	use "$output_data/IGNITE_community_survey_clean.dta", clear

	*Create a variable for survey wave
	gen wave2 = 0
	gen obs2 = _n
	replace wave2 = 1 if obs2 > 84
	
	gen tether = prob_parole
	replace tether = 0 if tether == .

	local outcomes good_thing ffgood_thing selfgood_thing le_positive total_hopeful ff_hopeful_binary self_hopeful_binary 


	keep if lengthfactor2 != .	
	
	eststo clear
	local balrow1 site1 site2 site3 site4 site5
	
	local balrow2 male black white hispanic ///
	educ4 educ3 educ1 educ2 age1 age2 age3 age4 age5 age6

		est clear
			local demographics `balrow1' `balrow2'

********************************************************************************
* Balance Test
********************************************************************************			
			reghdfe ignite_expose `demographics' if lengthfactor2 !=., a(wave2)
						eststo bal_joint
						testparm `demographics'
						local fp = r(p)
						local fp: di %8.3f `fp'
						local fp = subinstr("`fp'"," ","",.)
						
						local fs = r(F)
						local fs: di %8.3f `fs'
						local fs = subinstr("`fs'"," ","",.)
						local fs = " \multicolumn{3}{c}{`fs' [`fp']}"
						estadd local Fs "`fs'": bal_joint

					local Num = e(N)
					local Num : display %8s `Num'
					local clust = e(N_clust)
					local clust : display %8s `clust'
					
					local Num = "\multicolumn{3}{c}{`Num'}"
					local clust = "\multicolumn{3}{c}{`clust'}"
					local cont = "\multicolumn{3}{c}{Yes}"
					
					
					estadd local Num "`Num'" : bal_joint
					estadd local clust "`clust'" : bal_joint
					estadd local cont "`cont'": bal_joint
					
				foreach v of varlist `demographics'{
					reghdfe `v' ignite_expose if `v'>=0 & lengthfactor2 !=., vce(r) a(wave2)
					local `v'_b =_b[ignite_expose]
					local `v'_se = _se[ignite_expose]
					
					local `v'_b: display %8.3f ``v'_b'
					local `v'_se: display %8.3f ``v'_se'
					
					mat test = r(table)
					local p_val =  el(test,4,1)
						if `p_val'<0.01 {
							local star1="***"
						}
						if `p_val'>=0.01 & `p_val'<0.05 {
							local star1="**"
						}
						if `p_val'>=0.05 & `p_val'<0.1 {
							local star1="*"
						}
						if `p_val'>=0.1 {
							local star1=""
						}
					
					local `v'_b = "``v'_b'"+"`star1'"
					local `v'_se = subinstr("(``v'_se')"," ","",.)
						
					estadd local `v'_b "``v'_b'" : bal_joint
						
						
						su `v' if e(sample)==1
						
						local `v'_mean = r(mean)
						local `v'_sd = r(sd)
						local `v'_mean: display %8.3f ``v'_mean'
						local `v'_sd: display %8.3f ``v'_sd'
						local `v'_sd = subinstr("(``v'_sd')"," ","",.)
						
						local `v'_se = "&"+ "``v'_se'"
						local `v'_diff = "``v'_mean'" + "&" + "``v'_b'" + "``v'_se'"
						
						estadd local `v'_diff "``v'_diff'" : bal_joint
						estadd local `v'_se "``v'_se'" : bal_joint
}
		

		estout bal_joint ///
						using "${tables}/TableBal_01192024_v3.tex", style(tex)  ///
						label nobaselevels collabels(none) mlabels(none) /// 
						drop(`demographics' _cons) ///
						cells(b(star fmt(%8.3f)) se(par fmt(%8.3f))) /// 
						starlevels(* 0.10 ** 0.05 *** 0.01) /// 
						stats(foo site1_diff site2_diff site3_diff site4_diff site5_diff ///
							foo foo male_diff black_diff white_diff hispanic_diff educ4_diff educ3_diff educ1_diff  ///
							educ2_diff age1_diff age2_diff age3_diff age4_diff age5_diff age6_diff foo  Fs Num, /// 
						labels("\textbf{Panel A: Site}"  "Barbershop" "Church" "General Store" "Other" "Social Security Office" ///
						" " "\textbf{Panel B: Demographics}"  "Male" "Black" "White" "Hispanic"  ///
						"Completed college or more" "High school degree or GED" "No high school degree" "Some college" ///
						"Age 18-24" "Age 25-34" "Age 35-44" "Age 45-54" "Age 55-64" "Age 65+" ///
						"\hline" "\emph{F}-Statistic for Joint Test [\emph{p}-value]" "Observations")) replace		
						